package com.tbm.console.proj.service.impl;

import com.tbm.console.proj.entity.LocalUserInfoEntity;
import com.tbm.console.proj.repository.LocalUserInfoRepository;
import com.tbm.console.proj.service.LocalUserInfoService;
import com.tbm.console.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


//说明 字符串：，作为替换字符串 LOWERLocalUserInfo                LocalUserInfo

@Service
public class LocalUserInfoServiceImpl implements LocalUserInfoService {

    @Autowired
    private LocalUserInfoRepository localUserInfoRepository;

    @Override
    public List<LocalUserInfoEntity> findAll() {
        return findAll(new PageData());
    }

    @Override
    public List<LocalUserInfoEntity> findAll(Integer start, Integer length) {
        return findAll(new PageData(),start,length);
    }

    @Override
    public List<LocalUserInfoEntity> findAll(PageData pageData) {
        Specification<LocalUserInfoEntity> sp = this.getLocalUserInfoSpecification(pageData);
        return localUserInfoRepository.findAll(sp);
    }

    @Override
    public List<LocalUserInfoEntity> findAll(PageData pageData, Integer start, Integer length) {
        Specification<LocalUserInfoEntity> sp = this.getLocalUserInfoSpecification(pageData);
        Sort sort = new Sort(Sort.Direction.ASC,"id");
        Pageable pageable = new PageRequest(start, length, sort);
        return localUserInfoRepository.findAll(sp,pageable).getContent();
    }

    @Override
    public long count() {
        return count(new PageData());
    }

    @Override
    public long count(LocalUserInfoEntity localUserInfo) {
        return localUserInfoRepository.count(Example.of(localUserInfo));
    }

    @Override
    public long count(PageData pageData) {
        Specification<LocalUserInfoEntity> sp = this.getLocalUserInfoSpecification(pageData);
        return localUserInfoRepository.count(sp);
    }

    @Override
    public boolean deleteById(Integer id) {
        localUserInfoRepository.deleteById(id);
        return false;
    }

    @Override
    public boolean deleteByIds(Iterable<LocalUserInfoEntity> iterable) {
        return false;
    }

    @Override
    public LocalUserInfoEntity save(LocalUserInfoEntity localUserInfo) {
        List<LocalUserInfoEntity> list = saveAll(Arrays.asList(localUserInfo));
        return list.size() == 0 ? new LocalUserInfoEntity() : list.get(0);
    }

    @Override
    public List<LocalUserInfoEntity> saveAll(List<LocalUserInfoEntity> localUserInfos) {
        return localUserInfoRepository.saveAll(localUserInfos);
    }

    @Override
    public LocalUserInfoEntity findById(Integer id) {
        LocalUserInfoEntity localUserInfo = new LocalUserInfoEntity();
        try{
            localUserInfo = localUserInfoRepository.findById(id).get();
        }catch (Exception e){

        }
        return localUserInfo;
    }

    @Override
    public List<LocalUserInfoEntity> findAllById(Integer[] ids) {
        return localUserInfoRepository.findAllById(Arrays.asList(ids));
    }

    @Override
    public boolean checkIsNull(Integer userId) {
        PageData param = new PageData();
        param.put("userId", userId);
        List<LocalUserInfoEntity> all = this.findAll(param);
        if(all.size()==1){
            return true;
        }else
            return false;
    }

    //通过Specification来获取自定义的查询条件
    private Specification<LocalUserInfoEntity> getLocalUserInfoSpecification(PageData pageData){
        return new Specification() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> preList = new ArrayList<Predicate>();
                if (null!=pageData.get("deleteFlag")) {
                    preList.add(criteriaBuilder.equal(root.get("deleteFlag"), pageData.get("deleteFlag").toString()));
                }
                if(null!=pageData.get("userId")){
                    preList.add(criteriaBuilder.equal(root.get("userId"), pageData.get("userId").toString()));
                }
                Predicate[] p = new Predicate[preList.size()];
                return criteriaBuilder.and(preList.toArray(p));
            }
        };
    }
}